<?php

Class news{

    private $page;

    public function __construct($page){

        if($page == ''){
            $this->page = 40;
        }else{
            $this->page = $page;
        }
    }

    public function mr_news(){

        $url = 'http://c.3g.163.com/nc/article/list/T1467284926140/0-'+$this->page+'.html';
        $result = $this->curl($url,$data ='');
        $datainfo = json_decode($result,true);
        $datainfoarray = $this ->combineDika($datainfo);
	    $array = [];
        $num = 0;
        foreach ($datainfoarray as $key => $value){
                           foreach ($value as $k => $v ){
                               if(isset($v['url']) && !empty($v['url'])) {
                                   $array[$num]['title'] = $v['title'];
                                   $array[$num]['digest'] = $v['digest'];
                                   $array[$num]['docid'] = $v['docid'];
                                   $array[$num]['source'] = $v['source'];
                                   $array[$num]['url_3w'] = isset($v['url']) ? $v['url'] : '';
                                   $array[$num]['imgsrc'] = $v['imgsrc'];
                                   $array[$num]['ptime'] = $v['ptime'];
                                   $num++;
                               }else{
                                   continue;
                               }
                           }
        }

        exit($array);
    }


    private function combineDika() {
        $data = func_get_args();
        $data = current($data);
        $cnt = count($data);
        $result = array();
        $arr1 = array_shift($data);
        foreach ($arr1 as $key => $item) {
            $result[] = array($item);
        }

        foreach ($data as $key => $item) {
            $result = $this->combineArray($result, $item);
        }
        return $result;
    }
    private function combineArray($arr1, $arr2) {
        $result = array();
        foreach ($arr1 as $item1) {
            foreach ($arr2 as $item2) {
                $temp = $item1;
                $temp[] = $item2;
                $result[] = $temp;
            }
        }
        return $result;
    }

    public function curl($url,$data)
    {
        $ch = curl_init();

        //设置cookie
        // curl_setopt($ch, CURLOPT_HTTPHEADER, [$this->cookie]);

        if(!empty($data)){
            //设置返回结果为流
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            //设置返回header
            curl_setopt($ch, CURLOPT_HEADER, true);

            /* 设置超时时间*/
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);

            /* 设置通信方式 */
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

            curl_setopt($ch, CURLOPT_URL, $url);

            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

            $return = curl_exec($ch);
            $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            $header = substr($return, 0, $header_size);
            $content = substr($return, $header_size);

            curl_close($ch);

            return  $content;
            //return [
            //    'header' => $header,
            //   'content' => $content
            //];
        }else{
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
            $return = curl_exec($ch);
            curl_close($ch);
            return $return;
        }

    }





}


$s = $_GET ? $_GET['page'] : "";

if($s != ""){
    $news = new news((int)$s);
}









?>